import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.io.File;

public class OpenFileWindow extends JPanel
{
	public OpenFileWindow()
    {
		this.setLayout(new BorderLayout());
		index = indexNewTab;
		indexNewTab++;
        file = null;
        tabbedPane = getTabbedPane();
        add(tabbedPane,BorderLayout.CENTER);
        add(getBottomPanel(),BorderLayout.SOUTH);
    }
    public OpenFileWindow(Patient patient_)
    {
    	this.setLayout(new BorderLayout());
        file = null;
        tabbedPane = getTabbedPane(patient_);
        add(tabbedPane,BorderLayout.CENTER);
        add(getBottomPanel(),BorderLayout.SOUTH);
    }
    public OpenFileWindow(Patient patient_,File file_)
    {
    	this.setLayout(new BorderLayout());
        file = file_;
        tabbedPane = getTabbedPane(patient_);
        add(tabbedPane,BorderLayout.CENTER);
        add(getBottomPanel(),BorderLayout.SOUTH);
        data.setSavedState(true);
        anthropometry.setSavedState(true);
        result.setSavedState(true);
    }
    public Patient getPatient()
    {
    	Patient patient = new Patient(data.getData(),anthropometry.getList(),result.getDiagnosticList());
    	patient.setIndexCheckup(anthropometry.getIndexCheckup());
        return patient; 
    }
    private JTabbedPane getTabbedPane()
    {
        JTabbedPane tabPane = new JTabbedPane(JTabbedPane.BOTTOM);
        data = new Data();
        anthropometry = new Anthropometry();
        result = new Result(new Patient());
        tabPane.add(Resources.getString("data"),new JScrollPane(data));
        tabPane.add(Resources.getString("anthropometry"),new JScrollPane(anthropometry));
        tabPane.add(Resources.getString("result"),new JScrollPane(result));
        tabPane.addChangeListener(new
          ChangeListener()
          {
             public void stateChanged(ChangeEvent event)
             {
                int n = tabbedPane.getSelectedIndex();
                if(n == 2)
                    result.update(getPatient());
             }
          }
        );
        return tabPane;
    }
    private JTabbedPane getTabbedPane(Patient patient)
    {
        JTabbedPane tabPane = new JTabbedPane(JTabbedPane.BOTTOM);
        data = new Data(patient.getPersonalData());
        anthropometry = new Anthropometry(patient.getAnthropometryList());
        result = new Result(patient);
        tabPane.add(Resources.getString("data"),new JScrollPane(data));
        tabPane.add(Resources.getString("anthropometry"),new JScrollPane(anthropometry));
        tabPane.add(Resources.getString("result"),new JScrollPane(result));
        tabPane.addChangeListener(new
          ChangeListener()
          {
             public void stateChanged(ChangeEvent event)
             {
                int n = tabbedPane.getSelectedIndex();
                if(n == 2)
                    result.update(getPatient());
             }
          }
        );
        return tabPane;
    }
    private JPanel getBottomPanel()
    {
        JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT,30,0));
        JButton backButton = new JButton(Resources.getString("back"));
        backButton.setFont(Resources.getFont(Resources.FONT_TYPE.LABELS));
        backButton.addActionListener( new 
            ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    int index = tabbedPane.getSelectedIndex();
                    if(0 < index)
                        tabbedPane.setSelectedIndex(index-1);
                }
            }
        );
        JButton nextButton = new JButton(Resources.getString("next"));
        nextButton.setFont(Resources.getFont(Resources.FONT_TYPE.LABELS));
        nextButton.addActionListener( new 
            ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    int index = tabbedPane.getSelectedIndex();
                    if(index < tabbedPane.getTabCount()-1)
                        tabbedPane.setSelectedIndex(index+1);
                }
            }
        );
        JPanel panel_aux1 = new JPanel(new GridLayout(1,2,30,0));
        panel_aux1.add(backButton);
        panel_aux1.add(nextButton);
        bottomPanel.add(panel_aux1);
        return bottomPanel;
    }
    public String getDiagnostic()
    {
        return result.getDiagnostic();
    }
    public File getFile()
    {
        return file;
    }
    public void setFile(File file_)
    {
        file = file_;
        data.setSavedState(true);
        anthropometry.setSavedState(true);
        result.setSavedState(true);
    }
    public String getFileTitle()
    {
    	if(file == null)
    	{
    		if(index == 0)
    			return Resources.getString("new");
    		else
    			return Resources.getString("new") + " " + index;
    	}else
    		return file.getName();
    }
    public void update(Patient patient)
    {
    	anthropometry.update(patient.getAnthropometryList(),patient.getIndexCheckup());
    }
    public void confirmNewValues()
    {
    	data.confirmNewValues();
    	anthropometry.confirmNewValues();
    	result.confirmNewValues();
    }
    public static final OpenFileWindow newOpenFileWindow()
    {
    	return new OpenFileWindow();
    }
    public boolean existNewInfo()
    {
    	if(data.existNewInfo())
    		return true;
    	if(anthropometry.existNewInfo())
    		return true;
    	if(result.existNewInfo())
    		return true;
    	return false;
    }
    public boolean getSavedState()
    {
    	if(!data.getSavedState())
    		return false;
    	if(!anthropometry.getSavedState())
    		return false;
    	if(!result.getSavedState())
    		return false;
		return true;
    }
    private JTabbedPane tabbedPane;
    private File file;
    private Data data;
    private Result result;
    private Anthropometry anthropometry;
    private static int indexNewTab = 0;
    private int index;
    //private Patient patient;
}
